package com.example.habits;

import com.example.datastructures.MyTime;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Broadcast receiver that detects unlocks.
 * 
 * @author Oskar
 */
public class UnlockReceiver extends BroadcastReceiver {

	/**
	 * Due to android API not really working (broadcasting many times) we need to check that
	 * the right broadcast is sent.
	 * 
	 * Inserts the current time in the database.
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(Intent.ACTION_USER_PRESENT) && intent.getComponent() != null &&
			intent.getComponent().getShortClassName().equals(".UnlockReceiver")) {
			
			System.out.println("HEJ");

			EntryDataSource eds = EntryDataSource.getInstance();
			eds.open();

			MyTime newTime = new MyTime(System.currentTimeMillis());
			System.out.println(newTime.getWeek());
			eds.addEntry(newTime.toLong(), newTime.toString(), newTime.getWeek());
			eds.close();
		}
	}
}
